package com.vincent.kdp.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.vincent.kdp.R;
import com.vincent.kdp.adapter.InStockExpandAdapter;
import com.vincent.kdp.bean.RxBusPosBean;
import com.vincent.kdp.http.RequestCallBack;
import com.vincent.kdp.repository.CheckGoodsRepository;
import com.vincent.kdp.ui.bean.response.BaseOutput;
import com.vincent.kdp.ui.bean.response.CounterOutput;
import com.vincent.kdp.ui.bean.response.InStockOutput;
import com.vincent.kdp.util.RxBus;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscription;
import rx.functions.Action1;

public class ScanResultActivity extends AppCompatActivity {

    @Bind(R.id.back)
    LinearLayout back;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.check_all)
    CheckBox checkAll;
    @Bind(R.id.title_rl)
    RelativeLayout titleRl;
    @Bind(R.id.message_lv)
    ExpandableListView messageLv;
    @Bind(R.id.list_md_text)
    TextView listMdText;
    @Bind(R.id.list_md_choose)
    Button listMdChoose;
    @Bind(R.id.list_md_send)
    Button listMdSend;

    private InStockExpandAdapter adapter;

    InStockOutput mbean;
    Subscription rxSbscription;
    String goodsIdList = "";
    String mdId = "0";
    CheckGoodsRepository checkGoodsRepository=new CheckGoodsRepository();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan_result);
        ButterKnife.bind(this);

        title.setText("待入柜列表");


        mbean = (InStockOutput) getIntent().getExtras().getSerializable("bean");

        adapter = new InStockExpandAdapter(this);
        messageLv.setAdapter(adapter);

        adapter.setList(mbean.getData(), checkAll);
        adapter.notifyDataSetChanged();

        checkAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    for (int i = 0; i < mbean.getData().size(); i++) {
                        mbean.getData().get(i).setGroupChecked("1");
                        for (int j = 0; j < mbean.getData().get(i).getGoodsList().size(); j++) {
                            mbean.getData().get(i).getGoodsList().get(j).setIsChecked("1");
                        }
                    }

                    adapter.notifyDataSetChanged();
                } else {
                    for (int i = 0; i < mbean.getData().size(); i++) {
                        mbean.getData().get(i).setGroupChecked("0");
                        for (int j = 0; j < mbean.getData().get(i).getGoodsList().size(); j++) {
                            mbean.getData().get(i).getGoodsList().get(j).setIsChecked("0");
                        }
                    }

                    adapter.notifyDataSetChanged();
                }
            }
        });
        rxBusObservers();
    }

    private void getIds() {
        for (int i = 0; i < mbean.getData().size(); i++) {
            for (int j = 0; j < mbean.getData().get(i).getGoodsList().size(); j++) {
                System.out.println("goodsIdList--j---"+goodsIdList);
                if (!"".equals(goodsIdList)) {

                    System.out.println("getStatus---"+mbean.getData().get(i).getGoodsList().get(j).getStatus());
                    System.out.println("getIsChecked---"+mbean.getData().get(i).getGoodsList().get(j).getIsChecked());
                    System.out.println("getInDepart---"+mbean.getData().get(i).getGoodsList().get(j).getInDepart());

                    if (Integer.parseInt(mbean.getData().get(i).getGoodsList().get(j).getStatus()) == 1 ||
                            Integer.parseInt(mbean.getData().get(i).getGoodsList().get(j).getStatus()) == 4) {
                        if (Integer.parseInt(mbean.getData().get(i).getGoodsList().get(j).getIsChecked()) == 1 &&
                                Integer.parseInt(mbean.getData().get(i).getGoodsList().get(j).getInDepart()) == 1
                                ) {
//                                if(goodsIdList.contains(mbean.getData().get(i).getGoodsList().get(j).getGoodsId()))
                            goodsIdList = goodsIdList + "," + mbean.getData().get(i).getGoodsList().get(j).getGoodsId();
                            System.out.println("goodsIdList-----"+goodsIdList);
                        }
                    }
                } else {
                    System.out.println("getStatus---"+mbean.getData().get(i).getGoodsList().get(j).getStatus());
                    System.out.println("getIsChecked---"+mbean.getData().get(i).getGoodsList().get(j).getIsChecked());
                    System.out.println("getInDepart---"+mbean.getData().get(i).getGoodsList().get(j).getInDepart());
                    if (Integer.parseInt(mbean.getData().get(i).getGoodsList().get(j).getStatus()) == 1 ||
                            Integer.parseInt(mbean.getData().get(i).getGoodsList().get(j).getStatus()) == 4) {
                        if (Integer.parseInt(mbean.getData().get(i).getGoodsList().get(j).getIsChecked()) == 1 &&
                                Integer.parseInt(mbean.getData().get(i).getGoodsList().get(j).getInDepart()) == 1
                                ) {
                            goodsIdList = mbean.getData().get(i).getGoodsList().get(j).getGoodsId();
                            System.out.println("goodsIdList--空---"+goodsIdList);
                        }
                    }
                }

            }
        }
    }

    private void rxBusObservers() {
        rxSbscription = RxBus.getInstance().toObserverable(RxBusPosBean.class)
                .subscribe(new Action1<RxBusPosBean>() {
                    @Override
                    public void call(RxBusPosBean bean) {
                        //T.showShort("11111111------"+String.valueOf(bean.getType()));
                        if (bean.getType() == 1) {
                            mdId = bean.getDepartId();
                            String name = bean.getName();
                            listMdText.setText(name);

                        }
                    }
                });
    }

    @Override
    protected void onDestroy() {
        if (!rxSbscription.isUnsubscribed()) {
            rxSbscription.unsubscribe();
        }
        super.onDestroy();
    }

    @OnClick({R.id.back, R.id.list_md_choose, R.id.list_md_send})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;

            case R.id.list_md_choose:
                checkGoodsRepository.getlistCounter(new RequestCallBack<CounterOutput>() {
                    @Override
                    public void onSuccess(CounterOutput bean) {

                        if (bean.getCode() == 0) {
                            Intent it = new Intent(ScanResultActivity.this,ChooseDepartActivity.class);
                            Bundle b = new Bundle();
                            b.putSerializable("bean", bean);
                            it.putExtras(b);
                            startActivity(it);
                        } else {

                        }

                    }

                    @Override
                    public void onFalse(String msg) {
                       // dialog(msg);
                    }
                });
                break;
            case R.id.list_md_send:
                if("0".equals(mdId)){
                    Toast.makeText(this,"请选择门店",Toast.LENGTH_SHORT).show();
                    return;
                }
                goodsIdList="";
                getIds();
                System.out.println("goodsIdList--传---"+goodsIdList);
                checkGoodsRepository.overTheCounter(goodsIdList,mdId,new RequestCallBack<BaseOutput>() {
                    @Override
                    public void onSuccess(BaseOutput bean) {

                        if (bean.getCode() == 0) {
                            if (bean.getCode() == 0) {
                                Toast.makeText(ScanResultActivity.this,"入柜成功",Toast.LENGTH_SHORT).show();


                            } else {
                                Toast.makeText(ScanResultActivity.this,bean.getMsg(),Toast.LENGTH_SHORT).show();
                            }
                        } else {

                        }

                    }

                    @Override
                    public void onFalse(String msg) {
                        // dialog(msg);
                    }
                });
                break;
        }
    }
}
